package com.mcxx.modules.temporary.service.impl;

import com.mcxx.common.exception.BizException;
import com.mcxx.modules.temporary.service.TempSalvationCompleteService;
import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.filemanage.easypoi.util.ExportExcelService;
import com.mcxx.modules.filemanage.file.service.FileService;
import com.mcxx.modules.miniSalvation.check.service.write.CheckWriteService;
import com.mcxx.modules.miniSalvation.order.service.write.InHouseWriteService;
import com.mcxx.modules.miniSalvation.order.service.write.NoticeBoardDetailWriteService;
import com.mcxx.modules.system.entity.TaskExportEntity;
import com.mcxx.modules.system.service.write.TaskExportWriteService;
import com.mcxx.modules.temporary.cancel.dto.TmpCancelSalvationDTO;
import com.mcxx.modules.temporary.cancel.entity.TmpCancelEntity;
import com.mcxx.modules.temporary.cancel.service.write.TmpCancelWriteService;
import com.mcxx.modules.temporary.family.service.write.TmpFamilyMainHistoryWriteService;
import com.mcxx.modules.temporary.fundpay.service.read.TmpFundPayFamilyReadService;
import com.mcxx.modules.temporary.order.dto.TmpOrderExcelDTO;
import com.mcxx.modules.temporary.order.entity.TmpOrderDetailEntity;
import com.mcxx.modules.temporary.order.entity.TmpOrderEntity;
import com.mcxx.modules.temporary.order.param.OrderListSearchParam;
import com.mcxx.modules.temporary.order.service.read.TmpOrderReadService;
import com.mcxx.modules.temporary.order.service.write.TmpOrderDetailWriteService;
import com.mcxx.modules.temporary.order.service.write.TmpOrderWriteService;
import com.mcxx.modules.temporary.salary.service.write.TmpFamilySalaryWriteService;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.Constant;
import com.mcxx.util.DateUtil;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@TaTransactional
public class TempSalvationCompleteServiceImpl extends BaseService implements TempSalvationCompleteService {

    @Resource
    private ExportExcelService exportExcelService;
    @Autowired
    private TmpFundPayFamilyReadService tmpFundPayFamilyReadService;
    @Autowired
    private FileService fileService;
    @Autowired
    private TaskExportWriteService taskExportWriteService;
    @Autowired
    private TmpOrderReadService tmpOrderReadService;
    @Autowired
    private TmpCancelWriteService tmpCancelWriteService;
    @Autowired
    private TmpOrderWriteService tmpOrderWriteService;
    @Autowired
    private InHouseWriteService inHouseWriteService;
    @Autowired
    private CheckWriteService checkWriteService;
    @Autowired
    private TmpOrderDetailWriteService tmpOrderDetailWriteService;
    @Autowired
    private NoticeBoardDetailWriteService noticeBoardDetailWriteService;
    @Autowired
    private TmpFamilySalaryWriteService tmpFamilySalaryWriteService;
    @Autowired
    private TmpFamilyMainHistoryWriteService tmpFamilyMainHistoryWriteService;


    @Override
    public ExcelInfo exportExcelSave(OrderListSearchParam param, UserAccountVo accountVo){
        ExcelInfo excelInfo = null;
        QueryParamUtil.setBaseParamValue(param,accountVo);
        param.setBusinessType(Constant.BusinessCode.LINSHI);
        if("1".equals(param.getExportType())){



            List<TmpOrderExcelDTO> list = tmpOrderReadService.exportListTown(param);
            excelInfo = exportExcelService.exportOrgOpLogs(TmpOrderExcelDTO.class,list,"临时救助乡镇已办信息.xls",
                    "临时救助乡镇已办列表","临时救助乡镇已办列表",false);
        }else if("2".equals(param.getExportType())){

            List<TmpOrderExcelDTO> list = tmpOrderReadService.exportListCounty(param);
            excelInfo = exportExcelService.exportOrgOpLogs(TmpOrderExcelDTO.class,list,"临时救助区县已办信息.xls",
                    "临时救助区县已办列表","临时救助区县已办列表",false);
        }


        return excelInfo;
    }


    @Override
    public int canCancelSalvation(String orderId){
        TmpOrderEntity entity = tmpOrderReadService.getById(orderId);
        if(!Constant.BizStateTmpNew.APPROVE_YES.equals(entity.getState())){
            throw new BizException("-1","不是审核通过业务不能作废");
        }
        String nowMonth = DateUtil.sdfYM.format(new Date());
        if(CommonUtil.invalidArgs(entity.getCheckDate()) || !nowMonth.equals(DateUtil.sdfYM.format(entity.getCheckDate()))){
            throw new BizException("-1","非本月办结业务不能作废");
        }
        return tmpFundPayFamilyReadService.selectFundCountByOrderId(orderId);
    }


    @Override
    public void handleCancelSalvation(TmpCancelSalvationDTO dto,UserAccountVo accountVo){

        TmpCancelEntity tmpCancelEntity = tmpOrderReadService.selectCancelInfo(dto.getOrderId());
        tmpCancelEntity.setId(Constant.getUUID());
        tmpCancelEntity.setOperator(accountVo.getLoginId());
        tmpCancelEntity.setCancelReason(dto.getCancelReason());
        tmpCancelEntity.setRemark(dto.getRemark());
        tmpCancelEntity.setOperDate(new Date());

        tmpCancelWriteService.insertIgnoreNull(tmpCancelEntity);

        String orderId = dto.getOrderId();

        tmpOrderWriteService.deleteById(orderId);

        inHouseWriteService.deleteByOrderId(orderId);

        checkWriteService.deleteByOrderId(orderId);

        TmpOrderDetailEntity orderDetailEntity = new TmpOrderDetailEntity();
        orderDetailEntity.setOrderId(orderId);
        tmpOrderDetailWriteService.delete(orderDetailEntity);

        noticeBoardDetailWriteService.deleteByOrderId(orderId);

        tmpFamilySalaryWriteService.deleteSalaryByOrderId(orderId);

        tmpFamilyMainHistoryWriteService.deleteByOrderId(orderId);
    }
}
